﻿using community.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace community.Controllers
{
    [Route("api/property-equipments")]
    [ApiController]
    public class PropertyEquipmentsController : ControllerBase
    {
        private static List<PropertyEquipment> equipments = new List<PropertyEquipment>();

        // 添加设备
        [HttpPost]
        public IActionResult AddEquipment([FromBody] PropertyEquipment equipment)
        {
            equipment.Id = equipments.Count + 1;
            equipment.CreatedAt = DateTime.UtcNow;
            equipment.UpdatedAt = DateTime.UtcNow;
            equipments.Add(equipment);
            return CreatedAtAction(nameof(GetEquipmentById), new { id = equipment.Id }, equipment);
        }

        // 查询所有设备
        [HttpGet]
        public IActionResult GetAllEquipments()
        {
            return Ok(equipments);
        }

        // 根据ID查询设备
        [HttpGet("{id}")]
        public IActionResult GetEquipmentById(int id)
        {
            var equipment = equipments.Find(e => e.Id == id);
            if (equipment == null)
            {
                return NotFound();
            }
            return Ok(equipment);
        }

        // 更新设备信息
        [HttpPut("{id}")]
        public IActionResult UpdateEquipment(int id, [FromBody] PropertyEquipment updatedEquipment)
        {
            var equipment = equipments.Find(e => e.Id == id);
            if (equipment == null)
            {
                return NotFound();
            }

            equipment.Name = updatedEquipment.Name;
            equipment.Location = updatedEquipment.Location;
            equipment.InstallDate = updatedEquipment.InstallDate;
            equipment.IsOperational = updatedEquipment.IsOperational;
            equipment.UpdatedAt = DateTime.UtcNow;

            return NoContent();
        }

        // 删除设备
        [HttpDelete("{id}")]
        public IActionResult DeleteEquipment(int id)
        {
            var equipment = equipments.Find(e => e.Id == id);
            if (equipment == null)
            {
                return NotFound();
            }

            equipments.Remove(equipment);
            return NoContent();
        }
    }
}
